<?php
namespace Opencart\Admin\Controller\Extension\ExportImport\Module;

class ExportImport extends \Opencart\System\Engine\Controller {

    private $codename = 'export_import';
    private $route = 'extension/export_import/module/export_import';
    private $config_file = 'export_import';
    private $extension = array();
    private $store_id = 0;
    private $error = array();
    
    public function __construct($registry) {
        parent::__construct($registry);
        $this->load->model($this->route);
        $this->load->model('extension/export_import/export_import/export');
        $this->load->model('extension/export_import/export_import/import');
        $this->load->language($this->route);

        $this->store_id = (isset($this->request->get['store_id'])) ? $this->request->get['store_id'] : 0;
    }
    
    public function index() {
        
        $user_token = '&user_token=' . $this->session->data['user_token'];
        
        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
            
            if(isset($this->request->post['filters'])){
                $this->export();
            }else{
                $this->model_setting_setting->editSetting($this->codename, $this->request->post, $this->store_id);
                $this->session->data['success'] = $this->language->get('text_success');

                $this->response->redirect($this->url->link('marketplace/extension', $user_token . '&type=module', true));
            }            
        }

        // styles and scripts
        $this->document->addScript(HTTP_CATALOG. 'extension/export_import/admin/view/javascript/bootstrap_switch/js/bootstrap-switch.min.js');
        $this->document->addStyle(HTTP_CATALOG. 'extension/export_import/admin/view/javascript/bootstrap_switch/css/bootstrap-switch.css');
        $this->document->addScript(HTTP_CATALOG. 'extension/export_import/admin/view/javascript/export_import/bootstrap.min.js');
        $this->document->addScript(HTTP_CATALOG. 'extension/export_import/admin/view/javascript/export_import/library/jquery.serializejson.min.js');
        $this->document->addScript(HTTP_CATALOG. 'extension/export_import/admin/view/javascript/riot/riot+compiler.min.js');
        $this->document->addScript(HTTP_CATALOG. 'extension/export_import/admin/view/javascript/export_import/export_import.js');

        // Variable
        $data['codename'] = $this->codename;
        $data['route'] = $this->route;

        // Setup
        $data['setup'] = $this->isSetup();
        $data['background_image'] = HTTP_CATALOG . "extension/export_import/admin/view/image/export_import/bg.svg";
        $data['icon_image'] = HTTP_CATALOG . "extension/export_import/admin/view/image/export_import/icon.svg";
        $data['setup_link'] = $this->url->link($this->route.'|setup'.$user_token, '', true);
        $data['title_setup'] = $this->language->get('title_setup');
        $data['text_setup'] = $this->language->get('text_setup');

        //title
        $data['heading_title'] = $this->language->get('heading_title_main');

        // Exel
        $data['action'] = $this->url->link($this->route,'&user_token=' . $this->session->data['user_token'], true);

        $json['translate']['text_memory_usage'] = $this->language->get('text_memory_usage');
        $json['translate']['text_progress'] = $this->language->get('text_progress');
        $json['translate']['text_current_item'] = $this->language->get('text_current_item');
        $json['translate']['text_progress_sheets'] = $this->language->get('text_progress_sheets');
        $json['translate']['text_progress_files'] = $this->language->get('text_progress_files');
        $json['translate']['text_left_time'] = $this->language->get('text_left_time');
        $json['translate']['text_success_title'] = $this->language->get('text_success_title');
        $json['translate']['text_error_title'] = $this->language->get('text_error_title');
        $json['translate']['text_please_wait'] = $this->language->get('text_pleas_wait');
        $json['translate']['text_filter_title'] = $this->language->get('text_filter_title');
        $json['translate']['text_not_selected'] = $this->language->get('text_not_selected');
        $json['translate']['text_success_import'] = $this->language->get('text_success_import');
      
        $data['entry_source'] = $this->language->get('entry_source');
        $data['entry_language'] = $this->language->get('entry_language');
        $data['text_exel'] = $this->language->get('text_title');
        $data['column_name'] = $this->language->get('column_name');
        $data['column_description'] = $this->language->get('column_description');
        $data['column_action'] = $this->language->get('column_action');

        $data['button_export'] = $this->language->get('button_export');
        $data['button_import'] = $this->language->get('button_import');
        $data['button_filter'] = $this->language->get('button_filter');
        $json['translate']['button_close'] = $this->language->get('button_close');
        $data['cancel'] = $this->url->link('marketplace/extension', '&user_token=' . $this->session->data['user_token'] . '&type=module', true);
        $data['button_cancel'] = $this->language->get('button_cancel');
        $data['languages'] = $this->model_extension_export_import_module_export_import->getLanguages();  
        
        //Suport
        $data['text_ticket'] =  $this->language->get('text_ticket');
        $data['entry_ticket'] =  $this->language->get('entry_ticket');
        $data['text_powered_by'] =  $this->language->get('text_powered_by');

        //setting
        $data['entry_limit'] = $this->language->get('entry_limit');
        $data['entry_limit_step'] = $this->language->get('entry_limit_step');
        $data['entry_truncate_table'] = $this->language->get('entry_truncate_table');
        $data['help_limit'] = $this->language->get('help_limit');
        $data['help_limit_step'] = $this->language->get('help_limit_step');
        $data['help_truncate_table'] = $this->language->get('help_truncate_table');
        $data['text_settings'] = $this->language->get('text_settings');

        $data['text_complete_version'] = $this->language->get('text_complete_version');

        $modules = $this->model_extension_export_import_module_export_import->getModules();
        
        $json['selected_filters'] = array();
        
        $data['modules'] = array();
        $json['filters'] = array();

        $json['conditions'] = array(
            '=' => $this->language->get('text_equal'),
            '<>' => $this->language->get('text_not_equal'),
            '>=' => $this->language->get('text_greater_or_equal'),
            '<=' => $this->language->get('text_less_or_equal'),
            '>' => $this->language->get('text_greater'),
            '<' => $this->language->get('text_less'),
            'LIKE' => $this->language->get('text_like')
            );
       
        foreach ($modules as $value) {
            $module_setting = $this->model_extension_export_import_module_export_import->getModuleSetting($value);
            
            $json['filters'][$value] = $this->model_extension_export_import_module_export_import->getModuleFilters($value);

            $this->load->language('extension/export_import/export_import_module/'.$value);

            $data['modules'][$value] = array(
                'title' => $this->language->get('text_title'),
                'description' => $this->language->get('text_description')
                );
        }

        $json['server'] = HTTP_CATALOG;

        $setting = $this->model_setting_setting->getSetting($this->codename);

        if (isset($this->request->post[$this->codename.'_setting'])) {
            $data['setting'] = $this->request->post[$this->codename.'_setting'];
        } else if(isset($setting[$this->codename.'_setting'])) {
            $data['setting'] = $setting[$this->codename.'_setting'];
        } else {
            $this->load->config($this->codename);
            $data['setting'] = $this->config->get($this->codename.'_setting');
        }

        // Breadcrumbs
        $data['breadcrumbs'] = array();
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', $user_token)
        );

        $data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_module'),
            'href'      => $this->url->link('marketplace/extension', $user_token)
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title_main'),
            'href' => $this->url->link($this->route, $user_token)
        );

        $data['riot_tags'] = $this->model_extension_export_import_module_export_import->getRiotTags();

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $data['json'] = json_encode($json);
     
        $this->response->setOutput($this->load->view($this->route, $data));
    }

    public function isSetup() {
        $setting_module = $this->model_setting_setting->getSetting('module_' . $this->codename);

    	if($setting_module) {
        	return true;
        }

    	return false;
    }

    public function setup() {

        $data['module_'.$this->codename.'_status'] = 1;

        $this->model_setting_setting->editSetting('module_' . $this->codename, $data);

        $data = [];

        $data[$this->codename.'_setting']['status'] = 1;
        $data[$this->codename.'_setting']['limit'] = 1;
        $data[$this->codename.'_setting']['limit_step'] = 50;
        $data[$this->codename.'_setting']['truncate_table'] = 0;

        $this->model_setting_setting->editSetting($this->codename, $data);

        $this->session->data['success'] = $this->language->get('success_setup');
        $this->response->redirect($this->url->link($this->route, '&user_token=' . $this->session->data['user_token']));
    }

    public function export() {
        $json = array();
        
        if(isset($this->request->post['source'])){
            $source = $this->request->post['source'];
        }

        if(isset($this->request->post['export_import_setting']['language_id'])){
            $language_id = $this->request->post['export_import_setting']['language_id'];
        }

        if(isset($this->request->post['filters'])){
            $filters = $this->request->post['filters'];
        }
        else{
            $filters = array();
        }
   
        if(isset($source) && isset($language_id)){
            $json = $this->model_extension_export_import_export_import_export->export($source, $language_id, $filters);
        }
        else{
            $json['error'] = 'error';
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

    public function download() {
       
        $json = array();
        
        if(isset($this->request->get['source'])){
            $source = $this->request->get['source'];
        }

        if(isset($source)){
            $this->model_extension_export_import_module_export_import->save($source);
        }
        else{
            $json['error'] = 'error';
        }
    }

    public function import() {

        $json = array();

        if(isset($this->request->post['recipient'])){
            $recipient = $this->request->post['recipient'];
        }
        else{
            $json['error'] = 'error';
        }

        if(isset($this->request->post['export_import_setting']['language_id'])){
            $language_id = $this->request->post['export_import_setting']['language_id'];
        }
        else{
            $json['error'] = 'error';
        }

        if(!isset($json['error'])){
            $result = $this->model_extension_export_import_export_import_import->prepare_upload_file();
            if(!$result){
                $json['error'] = 'error';
            }
        }
        
        if(!isset($json['error'])){
            $json = $this->model_extension_export_import_export_import_import->import($recipient, $language_id);
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

}
